package com.wq.wrongquestion.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;
import com.wq.wrongquestion.entity.phy.PhysicsEntity;
import com.wq.wrongquestion.service.phy.PhysicsService;
import com.wq.wrongquestion.utils.FileUploadUtils;

@Controller
@RequestMapping("/phy")
public class PhysicsController {
    /************************************************Physics Start************************************************/

    @Autowired
    public PhysicsService physicsService;
    @Autowired
    public FileUploadUtils fileUploadUtils;
     
    final String IMG_PAHT_YT_PHY = "images/phy/yt/";
    final String IMG_PAHT_DA_PHY = "images/phy/da/";      
    

    @RequestMapping("/physicsInit") 
    public String physicsInit(Model model){
        //章节
        List<String> allZj=physicsService.getPhysicsZj();
        
        model.addAttribute("allZj", allZj);
        model.addAttribute("condEntity", new PhysicsEntity());
        
        return "phy/physicsSearch";
    }


    @PostMapping("/physicsIns")
    public String insPhysics(PhysicsEntity physicsEntity, @RequestParam("ytfileinfo") MultipartFile ytFile,
            @RequestParam("dafileinfo") MultipartFile daFile, Model model) {
        // 获取文件名
        String fileName = "yt" + System.currentTimeMillis() + "_" + ytFile.getOriginalFilename();
        String fileName2 = "da" + System.currentTimeMillis() + "_" + daFile.getOriginalFilename();

        // 获取文件路径
        String filePath = fileUploadUtils.getUploadFilePath(IMG_PAHT_YT_PHY);
        String filePath2 = fileUploadUtils.getUploadFilePath(IMG_PAHT_DA_PHY);

        // 保存文件
        try {
            if (ytFile.getSize() > 0) {
                fileUploadUtils.uploadFile(ytFile.getBytes(), filePath, fileName);
                physicsEntity.setImgytnm(fileName);
                physicsEntity.setImgytpath(filePath);
            }
            if (daFile.getSize() > 0) {
                fileUploadUtils.uploadFile(daFile.getBytes(), filePath2, fileName2);
                physicsEntity.setImgdanm(fileName2);
                physicsEntity.setImgdapath(filePath2);
            }
            // 插入数据
            physicsEntity.setFxcs(1);
            physicsEntity.setXueke(physicsEntity.getHidXueke());

            physicsService.insPhysics(physicsEntity);

            // return "redirect:/phy/physicsInit";
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            
            model.addAttribute("msg", "发生错误,请重试!");
            return "redirect:/phy/addPhysics";
        }
        return "redirect:/phy/physicsInit";
    }
    @RequestMapping("/addPhysics")
    public String addPhysics(Model model){
        List<String> allZj=physicsService.getPhysicsZj();
        
        model.addAttribute("allZj", allZj);
        return "phy/addPhysics";
    }

    @RequestMapping("/searchPhysics")
    public String searchPhysics(Model model, @RequestParam(value = "pageNm",defaultValue= "1") Integer pageNum,
            @RequestParam(value="pageSize" ,defaultValue= "10") Integer pageSize, PhysicsEntity physicsEntity) {

        PageInfo<PhysicsEntity> physics = physicsService.getPhysics(pageNum, pageSize, physicsEntity);
        List<String> allZj=physicsService.getPhysicsZj();

        model.addAttribute("allZj", allZj);
        model.addAttribute("physics", physics);
        model.addAttribute("condEntity", physicsEntity);

        return "phy/physicsSearch";
    }

    @RequestMapping("/searchPhysicsByID")
    public String searchPhysicsByID(Model model, @RequestParam(value = "pkey",defaultValue= "") Integer id) {

        List<PhysicsEntity> physicsByID = physicsService.getPhysicsByID(id);
        List<String> allZj=physicsService.getPhysicsZj();
        if(!physicsByID.isEmpty()){
            model.addAttribute("physics", physicsByID);
        }else{
            model.addAttribute("msg", "这条记录在数据库中不存在！");
            return "phy/physicsSearch";
        }
        
        model.addAttribute("allZj", allZj);

        return "phy/modifyPhysics";
    }

    @PostMapping("/physicsUpd")
    public String physicsUpd(Model model, PhysicsEntity physicsEntity,@RequestParam("ytfileinfo") MultipartFile ytFile,
            @RequestParam("dafileinfo") MultipartFile daFile) {
        try {
            // 图片比较
            if(ytFile != null && ytFile.getOriginalFilename() !=null){
                if(!ytFile.getOriginalFilename().startsWith("yt", 0)){
                    // 获取文件名
                    String fileName = "yt" + System.currentTimeMillis() + "_" + ytFile.getOriginalFilename();
                    // 获取文件路径
                    String filePath = fileUploadUtils.getUploadFilePath(IMG_PAHT_YT_PHY);
                    if (ytFile.getSize() > 0) {
                        fileUploadUtils.uploadFile(ytFile.getBytes(), filePath, fileName);
                        physicsEntity.setImgytnm(fileName);
                        physicsEntity.setImgytpath(filePath);
                    }
                }
            }
            // 图片比较
            if(daFile != null && daFile.getOriginalFilename() !=null){
                if(!daFile.getOriginalFilename().startsWith("da", 0)){
                    // 获取文件名
                    String fileName2 = "da" + System.currentTimeMillis() + "_" + daFile.getOriginalFilename();
                    // 获取文件路径
                    String filePath2 = fileUploadUtils.getUploadFilePath(IMG_PAHT_DA_PHY);
                    if (daFile.getSize() > 0) {
                        fileUploadUtils.uploadFile(daFile.getBytes(), filePath2, fileName2);
                        physicsEntity.setImgdanm(fileName2);
                        physicsEntity.setImgdapath(filePath2);
                    }                
                }
            }
            // 插入数据
            physicsEntity.setFxcs(1);
            physicsEntity.setXueke(physicsEntity.getHidXueke());

            Integer updRes=physicsService.updPhysics(physicsEntity);

            if(updRes==1){
                model.addAttribute("msg", "修改成功！");
            }else{
                model.addAttribute("msg", "修改失败！");
            } 
            List<PhysicsEntity> physicsByID = physicsService.getPhysicsByID(physicsEntity.getId());           
            if(physicsByID.isEmpty()){
               physicsByID = new ArrayList<PhysicsEntity>();
            }
            model.addAttribute("physics", physicsByID);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            
            model.addAttribute("msg", "文件上传发生错误,请重试!");
            
        }


        return "phy/modifyPhysics";   
    }

    @PostMapping("/physicsDel")
    public String physicsDel(Model model, @RequestParam(value = "pkey",defaultValue= "") Integer id) {
        physicsService.delPhysics(id);
        
        return "redirect:/phy/physicsInit";
    }    
    /************************************************Physics End************************************************/
}
